home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 010a / dietdisc.zip / DIETCHK.C < prev    next >
C/C++ Source or Header  |  1991-02-10  |  8KB  |  244 lines

  1.  
  2. #include <stdio.h>
  3. #include <dos.h>
  4. #include <dir.h>
  5. #include <io.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8.  
  9. #define  FALSE   0
  10. #define  TRUE    1
  11.  
  12. int     critical_error;
  13. void    interrupt (*oldint24)(void);
  14. char    diet_id[] = "lZdIeT";
  15. int     i, filecount, flag;
  16. int     bytes_read;
  17. int     error_flag;
  18. int     fixit;
  19. unsigned char *status_ptr;
  20. struct  ffblk fblock;
  21. char    drive[5];
  22. char    path[81];
  23. char    name[10];
  24. char    ext[5];
  25.  
  26. char    segment_rec[4000];
  27.  
  28. struct  {
  29.         char    signature[6];
  30.         long    filelen;
  31.         char    filler[6];
  32.         }
  33.         footprint;
  34.  
  35. struct  {
  36.         long    next_ctl;
  37.         long    prev_ctl;
  38.         char    filler[8];
  39.         struct  {
  40.                 long  location;
  41.                 int   length;
  42.                 }
  43.                 location_length[250];
  44.         }
  45.         control_rec;
  46.  
  47. /* -------------------------------------------------- */
  48.  
  49. void    interrupt int24(unsigned bp, unsigned di, unsigned si,
  50.                         unsigned ds, unsigned es, unsigned dx,
  51.                         unsigned cx, unsigned bx, unsigned ax,
  52.                         unsigned ip, unsigned cs, unsigned flags)
  53.         {
  54.         critical_error = TRUE;
  55.         ax = (ax & 0xFF00) | 0x03;
  56.         }
  57.  
  58. void    check_file(void)
  59.         {
  60.         char    fullname[101];
  61.         unsigned char next_byte;
  62.         long    actual_size;
  63.         long    ctl_file_position;
  64.         int     handle;
  65.         int     ctl_rec_counter;
  66.         int     corrected;
  67.  
  68.         critical_error = FALSE;
  69.         fnmerge(fullname, drive, path, fblock.ff_name, "");
  70.         actual_size = fblock.ff_fsize;
  71.  
  72.         if (!fixit)
  73.             handle = _open(fullname, O_RDONLY | O_BINARY);
  74.         else
  75.             handle = _open(fullname, O_RDWR   | O_BINARY);
  76.  
  77.         if (handle == -1)
  78.             {
  79.             printf("Error opening %s\n", fblock.ff_name);
  80.             return;
  81.             }
  82.         bytes_read = read(handle, &footprint, sizeof(footprint));
  83.         if (bytes_read != sizeof(footprint))
  84.             {
  85.             _close(handle);
  86.             return;
  87.             }
  88.         if (memcmp(&footprint.signature, diet_id, 6) != 0)
  89.             {
  90.             _close(handle);
  91.             return;
  92.             }
  93.  
  94.         filecount++;
  95.         printf("File %s -- actual size: %ld      fattened size: %ld\n",
  96.                 fblock.ff_name, actual_size, footprint.filelen);
  97.         ctl_rec_counter = 0;
  98.  
  99. process_control:
  100.         corrected = FALSE;
  101.         ctl_rec_counter++;
  102.         ctl_file_position = tell(handle);
  103.         bytes_read = read(handle, &control_rec, sizeof(control_rec));
  104.         if (bytes_read != sizeof(control_rec))
  105.             {
  106.             error_flag = TRUE;
  107.             printf("Error in ctrl rec %d in %s\n",
  108.                     ctl_rec_counter, fblock.ff_name);
  109.             if (critical_error)
  110.                 printf("Critical I/O error occurred.\n");
  111.             else
  112.             if (bytes_read >= 0)
  113.                 printf("%d bytes requested; %d were read.\n",
  114.                     (int) sizeof(control_rec), bytes_read);
  115.             else
  116.                 printf("Permission denied.\n");
  117.             _close(handle);
  118.             return;
  119.             }
  120.  
  121.         status_ptr = (unsigned char *) &control_rec;
  122.         status_ptr += 127;
  123.         next_byte = (unsigned char) *status_ptr;
  124.         status_ptr--;
  125.         if (*status_ptr != 0 && *status_ptr != 0xff)
  126.             {
  127.             printf("Status byte error in ctrl rec %d in %s\n",
  128.                     ctl_rec_counter, fblock.ff_name);
  129.             error_flag  = TRUE;
  130.             corrected   = TRUE;
  131.             *status_ptr = next_byte;
  132.             }
  133.  
  134.         for (i=0; i<250; i++)
  135.             {
  136.             if (control_rec.location_length[i].location > 1024000l)
  137.                 {
  138.                 error_flag = TRUE;
  139.                 printf("Ctl rec %d; segment %d error in %s (loc. out of bounds)\n",
  140.                             ctl_rec_counter, i, fblock.ff_name);
  141.                 control_rec.location_length[i].location = -1;
  142.                 control_rec.location_length[i].length   = -1;
  143.                 corrected = TRUE;
  144.                 continue;
  145.                 }
  146.             if (control_rec.location_length[i].length != -1)
  147.                 {
  148.                 if (control_rec.location_length[i].length > 2048 + 256)
  149.                     {
  150.                     error_flag = TRUE;
  151.                     printf("Ctl rec %d; segment %d error in %s\n",
  152.                                 ctl_rec_counter, i, fblock.ff_name);
  153.                     printf("%d bytes (too large)\n",
  154.                         control_rec.location_length[i].length);
  155.                     control_rec.location_length[i].location = -1;
  156.                     control_rec.location_length[i].length   = -1;
  157.                     corrected = TRUE;
  158.                     continue;
  159.                     }
  160.                 critical_error = FALSE;
  161.                 lseek(handle, control_rec.location_length[i].location, 0);
  162.                 bytes_read = read(handle, segment_rec,
  163.                                   control_rec.location_length[i].length);
  164.                 if (bytes_read != control_rec.location_length[i].length)
  165.                     {
  166.                     error_flag = TRUE;
  167.                     printf("Ctl rec %d; segment %d error in %s\n",
  168.                                 ctl_rec_counter, i, fblock.ff_name);
  169.                     if (critical_error)
  170.                         printf("Critical I/O error occurred.\n");
  171.                     else
  172.                     if (bytes_read >= 0)
  173.                         {
  174.                         printf("%d bytes requested; %d were read.\n",
  175.                             control_rec.location_length[i].length,
  176.                             bytes_read);
  177.                         control_rec.location_length[i].location = -1;
  178.                         control_rec.location_length[i].length   = -1;
  179.                         corrected = TRUE;
  180.                         }
  181.                     else
  182.                         printf("Permission denied.\n");
  183.                     }
  184.                 }
  185.             }
  186.  
  187.         if (error_flag && fixit && corrected)
  188.             {
  189.             lseek(handle, ctl_file_position, 0);
  190.             write(handle, &control_rec, sizeof(control_rec));
  191.             }
  192.  
  193.         if (control_rec.next_ctl != 0l)
  194.             {
  195.             lseek(handle, control_rec.next_ctl, 0);
  196.             goto process_control;
  197.             }
  198.  
  199.         _close(handle);
  200.         }
  201.  
  202. void    main(int argc, char *argv[])
  203.         {
  204.         oldint24 = getvect(0x24);
  205.         setvect(0x24, int24);
  206.  
  207.         if (argc < 2)
  208.             {
  209.             printf("USAGE: DIETCHK <filespec>\n");
  210.             return;
  211.             }
  212.  
  213.         if (argc == 3)
  214.             if (stricmp(argv[2], "/f") == 0)
  215.                 fixit = TRUE;
  216.  
  217.         fnsplit(argv[1], drive, path, name, ext);
  218.         if ( (flag = findfirst(argv[1], &fblock, 0)) != 0 )
  219.             {
  220.             printf("File(s) not found.\n");
  221.             return;
  222.             }
  223.  
  224.         while (flag == 0)
  225.             {
  226.             check_file();
  227.             flag = findnext(&fblock);
  228.             }
  229.  
  230.         setvect(0x24, oldint24);
  231.  
  232.         if (error_flag)
  233.             {
  234.             if (!fixit)
  235.                 printf("\nErrors found; corrections not written to disk.\n");
  236.             else
  237.                 printf("\nErrors found; file(s) corrected (truncated).\n");
  238.             }
  239.         else
  240.             printf("\nNo errors found.\n");
  241.         }
  242.  
  243.  
  244.